<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title></title>
<link rel="stylesheet" href="admin/css/pintuer.css">
<link rel="stylesheet" href="admin/css/admin.css">
<script src="admin/js/jquery.js"></script>
<script src="admin/js/pintuer.js"></script>
<script src="ckeditor/ckeditor.js"></script>
<script src="ckfinder/ckfinder.js"></script>
<script src="ckeditor/samples/js/sample.js" type="text/javascript"></script>
<link href="ckeditor/samples/sample.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function(){
	$('#selectfiletype').change(function(){
		var p = $(this).children('option:selected').val();
		$("#showImg").attr("src", "");
		$("#isneturl").attr("value","1");
		$("#url2").val("");
		if(p != null){
// 			alert("选中的值："+p);
			if(p == 1 || p ==2){
				$("#imgfmdiv").css("display","none");
			}
			if(p ==1){
				$("#isneturl").attr("value","1");
				$("#url2").attr("type","file");
				$("#showImgdiv").css("display","block");
			}else if(p == 2){
				$("#isneturl").attr("value","2");
				$("#url2").attr("type","text");
				$("#url2").attr("name","resource");
				$("#url2").parent().find('.tipss').html('请输入网络图片url');
				$("#isneturl").attr("value","2");
			}else if(p == 4){
				$("#imgfmdiv").css("display","block");
				$("#showImgdiv").css("display","none");
				$("#url2").attr("type","text");
				$("#url2").attr("name","resource");
				$("#url2").parent().find('.tipss').html('请输入网络视频url');
				$("#isneturl").attr("value","4");
			}else if(p == 3){
				$("#isneturl").attr("value","3");
				$("#showImgdiv").css("display","none");
				$("#imgfmdiv").css("display","block");
				$("#url2").attr("type","file");
				}
		}else{
			$("#imgfmdiv").css("display","none");
			alert("未选择文件类型");
		}
		
	});
	
	$("#url2").change(function(){
		//图片预览方法
		var objUrl = null;
		var options_value=$("#selectfiletype option:selected").val();
		if(options_value == ""){
			alert("请选择文件类型");
			return;
		}
		var filepath = $("#url2").val();
		if(options_value == 2 || options_value == 4){
			objUrl = filepath;
		}else{
			objUrl = getObjectURL(this.files[0]) ;
		}
		if(filepath == null){
			alert("您的文件不正确，请重新选择");
			return;
		}
		var filetype = filepath.substring(filepath.lastIndexOf(".")+1);
		if (objUrl) {
			if(filetype.indexOf("jpg")>=0 ||filetype.indexOf("png")>= 0 || filetype.indexOf("bmp")>= 0
					|| filetype.indexOf("jpeg")>= 0 || filetype.indexOf("gif")>= 0 || filetype.indexOf("ico")>= 0){
				$("#showImg").attr("src", objUrl) ;
				$("#showVediodiv").css("display","none");
				$("#showSwfdiv").css("display","none");
			}else {
				$("#showImgdiv").css("display","none");
				if(!checkHtml5() || filetype.indexOf("3gp") >= 0 || filetype.indexOf("avi") >= 0 || filetype.indexOf("wmv") >= 0 
						|| filetype.indexOf("mpeg") >= 0 || filetype.indexOf("mkv") >= 0 || filetype.indexOf("rmvb") >= 0
						|| filetype.indexOf("mov") >= 0 || filetype.indexOf("swf") >= 0 || filetype.indexOf("flv") >= 0){
					$("#showSwfdiv").css("display","block");
					$("#showSwf").attr("src", objUrl) ;
				}else if(filetype.indexOf("mp4") >= 0 || filetype.indexOf("webm") >= 0 || filetype.indexOf("ogg") >= 0){
					$("#showVedio").attr("src", "") ;
					$("#showVediodiv").css("display","block");
					$("#showVedio").attr("src", objUrl) ;
				}else{
					alert("不支持该文件类型，请重新选择文件");
					return;
				}
			}
		}
	});
	$('#uadv_form').bind('submit',function(){
		var formData = new FormData();
		var file = $("#url2")[0].files[0];
		var file1 = $("#url3")[0].files[0];
		var title = $('input[name="title"]').val();
		var id = $('input[name="id"]').val();
		var addOrUpdate = $('input[name="addOrUpdate"]').val();
		var content = CKEDITOR.instances.TextArea1.getData();
		var type = $('#selectfiletype').val();
		var serviceId = $("#selectServerBelong").val();
		if(file != null){
			formData.append('file',file);
		}
		if(file1 != null){
			formData.append('file',file1);
		}
		if(type != null || type !='0'){
			formData.append('isneturl',type);
		}
		formData.append('id',id);
		formData.append('title',title);
		formData.append('addOrUpdate',addOrUpdate);
		formData.append('content',content);
		formData.append('parentId',serviceId);
		$.ajax({
			url:'admin/addExamples.do',
			type:'post',
			data:formData,
			dataType:'text',
			cache: false,  
	        contentType: false,  
	        processData: false,
			success:function(responseText){
				if(responseText.length > 0){
					alert(responseText);
					clearData();
					window.location.reload();
				}
			}
		});
		return false;
		
});
});
//建立一个可存取到该文件的URL
function getObjectURL(file) {
	var url = null ; 
	if (window.createObjectURL!=undefined) { // basic
		url = window.createObjectURL(file) ;
	} else if (window.URL!=undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file) ;
	} else if (window.webkitURL!=undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file) ;
	}
	return url ;
}
function clearData(){
	document.getElementById('add').scrollIntoView();
	$("#url2").attr("src",'');
	$('#addorupdate').html('增加内容');
	$('input[name="id"]').val('');
	$('input[name="addOrUpdate"]').val('2');
	$('input[name="title"]').val('');
	CKEDITOR.instances.TextArea1.setData('');
	$('#showImg').attr('src','');
	$("#selectfiletype").attr("value",'');
	$("#selectServerBelong").attr("value",'');
	CKEDITOR.instances.TextArea1.setData('');
}
//更新服务
function fillContext(id,resource ,type,isneturl,fmResource ,title,parentId,count ){
	$('#addorupdate').html('修改内容');
	$('input[name="id"]').val(id);
	$('input[name="isneturl"]').val(isneturl);
	$('input[name="title"]').val(title);
	$('input[name="addOrUpdate"]').val('1');
	var content = $('#contentdiv'+count).html();
	CKEDITOR.instances.TextArea1.setData(content);
	$('#showImg').attr('src',resource);
	if(isneturl == 1 || isneturl == 2){
		$("#showImg").attr('src',resource);
	}else if(isneturl == 3 || isneturl == 4){
		$("#showImg").attr('src',fmResource);
		if(type == 3){
			$("#showSwfdiv").css("display","block");
			$("#showVediodiv").css("display","none");
			$("#showSwf").attr("src",resource);
		}else if(type == 2){
			$("#showSwfdiv").css("display","none");
			$("#showVediodiv").css("display","block");
			$('#showVedio').attr('src',resource);
		}
	}
	$("#selectfiletype option").each(function(){
		var val = $(this).val();
		if(val == isneturl){
			$(this).attr("selected",true);
			return false;
		}
	});
	$("#selectServerBelong option").each(function(){
		var val = $(this).val();
		if(val == parentId){
			$(this).attr("selected",true);
			$("#selectServerBelong").trigger("change");
			return false;
		}
	});
}
//删除服务
function del(id){
	if(confirm("确定删除id为"+id+"的修复实例？")){
		$.ajax({url:'admin/delExampleById.do?id='+id,context: document.body,success:function(data){
			alert(data);
			window.location.reload();
		}})
	}
}
function checkHtml5(){
	if(window.applicationCache){
		return true;
	}
	else{
		return false;
	}
}
//admin/addServices.do
</script> 
</head>
<body>
<div class="panel admin-panel">
  <div class="panel-head"><strong class="icon-reorder"> 修复实例列表</strong></div>
  <div class="padding border-bottom">  
  <button type="button" class="button border-yellow" onclick="document.getElementById('add').scrollIntoView();"><span class="icon-plus-square-o"></span> 添加内容</button>
  </div>
  <table class="table table-hover text-center">
    <tr>
      <th width="1%">ID</th>
      <th width="15%">封面</th>
      <th width="10%">资源类型</th>
      <th width="15%">名称</th>
      <th width="15%">描述</th>
      <th width="20%">案例所属</th>
      <th width="15%">操作</th>
    </tr>
   	<c:if test="${!empty examples }">
   		<c:forEach items="${examples }" var="examples" varStatus="a">
   			<tr>
      			<td>${examples.id }</td>     
      			<td>
      			<c:if test="${examples.type == 1 }">
      			<a href="admin/getExampleById.do?id=${examples.id }"><img src="${examples.resource }" alt="${examples.title }" width="120" height="50" /></a>   
      			</c:if>
      			<c:if test="${examples.type != 1 }">
      			<a href="admin/getExampleById.do?id=${examples.id }"><img src="${examples.fmResource }" alt="${examples.title }" width="120" height="50" /></a>
      			</c:if>
      			</td>     
      			<td>${examples.typeName }</td>     
      			<td>${examples.title }</td>
      			<td>
      				<c:if test="${fn:containsIgnoreCase(examples.content, '<img ') }">
      					图文内容，点击修改查看详情
      					<div id="contentdiv${a.count }" style="display: none;">${examples.content }</div>
      				</c:if>
      				<c:if test="${!fn:containsIgnoreCase(examples.content,'<img ') }">
      					${examples.content }
      				</c:if>
      				
      			</td>
      			<td>${examples.parent.title }</td>
      			<td><div class="button-group">
      			<a class="button border-main" href="javascript:void(0)" onclick="fillContext('${examples.id}','${examples.resource }','${examples.type }','${examples.isneturl }','${examples.fmResource }','${examples.title }','${examples.parentId}','${a.count }')"><span class="icon-edit"></span> 修改</a>
      			<a class="button border-red" href="javascript:void(0)" onclick="del(${examples.id})"><span class="icon-trash-o"></span> 删除</a>
      			</div></td>
    		</tr>
   		</c:forEach>
   	</c:if>
  </table>
</div>
<div class="panel admin-panel margin-top" id="add">
  <div class="panel-head"><strong><span class="icon-pencil-square-o"></span> <b id="addorupdate">增加内容</b></strong></div>
  <div class="body-content">
    <form id="uadv_form" method="post" class="form-x" enctype="multipart/form-data"> 
    	<input type="hidden" name="id" value="0"/>   
    	<input type="hidden" name="addOrUpdate" value="2"/>
    	<input id="isneturl" type = "hidden" name="isneturl" value = "0">
      <div class="form-group">
        <div class="label">
          <label>名称：</label>
        </div>
        <div class="field">
          <input type="text" class="input w50" name="title" data-validate="required:请输入名称" />
          <div class="tips"></div>
        </div>
      </div>
      <div class="form-group">
          <div class="label">
            <label>文件类型：</label>
          </div>
          <div class="field">
            <select name="filetype" class="input w50" id="selectfiletype">
              <option value="1">本地图片</option>
            </select>
            <div class="tips"></div>
          </div>
          
        </div>
      <div class="form-group">
          <div class="label">
            <label>案例所属：</label>
          </div>
          <div class="field">
            <select name="filetype" class="input w50" id="selectServerBelong">
              <option value="0">选择服务所属</option>
              <c:forEach items="${services }" var="s">
              	<option value="${s.id }">${s.title }</option>
              </c:forEach>
            </select>
            <div class="tips"></div>
          </div>
          
        </div>
      <div class="form-group">
        <div class="label">
          <label>文件：</label>
        </div>
        <div class="field">
          <input type="file" id="url2" name="file" class="input tips" style="width:25%; float:left;"  data-place="right" />
          <div class="tipss">文件大小：1920*500</div>
        </div>
      </div>
      
      <div class="form-group" id = "imgfmdiv" style="display: none;">
        <div class="label">
          <label>文件封面：</label>
        </div>
        <div class="field">
          <input type="file" id="url3" name="file" class="input tips" style="width:25%; float:left;"  data-place="right" />
          <div class="tipss">文件大小：1920*500</div>
        </div>
      </div>
      <div class="form-group" id = "showImgdiv">
        <div class="label">
          <label>封面预览：</label>
        </div>
<!--         <div class="tip"> -->
        	<img id="showImg" src="" width="240" height="100"/>
<!--         </div> -->
      </div>
      <div class="form-group" id = "showVediodiv" style="display: none;">
        <div class="label">
          <label>Vedio预览：</label>
        </div>
		<video  id="showVedio" src="" controls="controls" preload="none" width="550" height="400"></video>
<!--         <div class="tipss">支持：3gp,rmvb,flv,wmv,avi,mkv,mp4,wav格式</div> -->
      </div>
      <div class="form-group" id = "showSwfdiv" style="display: none;">
        <div class="label">
          <label>视频预览：</label>
        </div>
        <embed type="application/x-shockwave-flash" 
        src="" width="550" height="400" 
        style="undefined" id="showSwf" name="sotester" 
        quality="high" wmode="transparent" scale="noscale" 
        menu="false" allowfullscreen="true" flashvars="lm=">
      </div>
      <div class="form-group">
        <div class="label">
          <label>描述：</label>
        </div>
        <div class="field">
<!--           <textarea type="text" class="input" name="content" style="height:120px;"></textarea> -->
          <textarea class="ckeditor" name="content" id="TextArea1" cols="20" rows="2"></textarea>
          <div class="tips"></div>
        </div>
      </div>
      <c:if test="${! empty addRslt }">
      <div id="uresultdiv" class="form-group" style="display: none;">
        <div class="label">
          <label for="sitename">更新结果：</label>
        </div>
        <div class="label" style="width: 300px;">
          <div id="uresult" style="color:red;">${addRslt }</div>
        </div>
      </div>
      </c:if>
      <div class="form-group">
        <div class="label">
          <label></label>
        </div>
        <div class="field">
          <input class="button bg-main icon-check-square-o" type="submit" value="提交"/>
        </div>
      </div>
    </form>
  </div>
</div>
<script type="text/javascript">
	CKEDITOR.replace('TextArea1');
</script>
</body></html>